//
//  NewFriendViewController.m
//  YMIM
//
//  Created by admin on 2022/3/24.
//
#import "NewFriendViewController.h"
#import <Masonry/Masonry.h>
#import "NewFriendTableViewCell.h"
#import "UIViewController+HUD.h"
#import <YMIMKit/YMIMKit.h>
@interface NewFriendViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UIImageView *emptyImageView;
@property (nonatomic,strong)UILabel *emptyLabel;
@end

@implementation NewFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新的好友";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self setEmptyView];
    self.dataArray = [NSMutableArray array];
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)loadData{
    
    [[YMIMClient sharedClient].friendManager getFriendsApplyListWithPage:1 pageSize:20 completion:^(NSArray<YMFriendApplyInfo *> * _Nonnull applyList, YMErrorCode code) {
        
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:applyList];
        if (self.dataArray.count==0) {
            
            self.emptyImageView.hidden = NO;
            self.emptyLabel.hidden = NO;
            
        }else{
            
            self.emptyImageView.hidden = YES;
            self.emptyLabel.hidden = YES;
        }
        [self.tableView reloadData];
     
    }];

}
-(void)setEmptyView{
    
    self.emptyImageView = [[UIImageView alloc]init];
    [self.emptyImageView setImage:[UIImage imageNamed:@"Contact_Null"]];
    [self.view addSubview:self.emptyImageView];
    [self.emptyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.mas_equalTo(self.view.mas_top).offset(SCREEN_HEIGHT/2-100);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(100);
    
    }];
    self.emptyImageView.hidden = YES;
   
    self.emptyLabel= [[UILabel alloc] init];
    self.emptyLabel.text = @"暂无新的好友";
    [self.emptyLabel setFont:YMFont(14)];
    [self.emptyLabel setTextColor:[UIColor lightGrayColor]];
    self.emptyLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.emptyLabel];
    [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    
         make.top.mas_equalTo(self.emptyImageView.mas_bottom).offset(5);
         make.centerX.mas_equalTo(self.emptyImageView.mas_centerX);
    
    }];
    self.emptyLabel.hidden = YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"cell";
    NewFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        
        cell = [[NewFriendTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }

    [cell.agreeButton addTarget:self action:@selector(agreeFriend:) forControlEvents:UIControlEventTouchUpInside];
    cell.agreeButton.tag = indexPath.row;
    [cell.refuseButton addTarget:self action:@selector(refuseFriend:) forControlEvents:UIControlEventTouchUpInside];
    cell.refuseButton.tag = indexPath.row;
    cell.backgroundColor =  [UIColor whiteColor];
    cell.applyInfo = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
-(void)agreeFriend:(UIButton *)sender{
    
    YMFriendApplyInfo *info = [self.dataArray objectAtIndex:sender.tag];
    [[YMIMClient sharedClient].friendManager handleFriendApplyWithApplyId:info.applyId isAgree:@"2" completion:^(YMErrorCode code) {
        
        if (self.handleBlock) {
            
            self.handleBlock();
        }
        [self showHint:@"已同意好友请求"];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
        NewFriendTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.agreeButton.backgroundColor = [UIColor grayColor];
        cell.agreeButton.userInteractionEnabled = NO;
        cell.refuseButton.backgroundColor = [UIColor grayColor];
        cell.refuseButton.userInteractionEnabled = NO;
        [self.tableView reloadData];
        
    }];
     
}
-(void)refuseFriend:(UIButton *)sender{
    
    YMFriendApplyInfo *info = [self.dataArray objectAtIndex:sender.tag];
    [[YMIMClient sharedClient].friendManager handleFriendApplyWithApplyId:info.applyId isAgree:@"3" completion:^(YMErrorCode code) {
        
        if (self.handleBlock) {
            
            self.handleBlock();
        }
        [self showHint:@"已拒绝好友请求"];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
        NewFriendTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.agreeButton.backgroundColor = [UIColor grayColor];
        cell.agreeButton.userInteractionEnabled = NO;
        cell.refuseButton.backgroundColor = [UIColor grayColor];
        cell.refuseButton.userInteractionEnabled = NO;
        [self.tableView reloadData];
        
    }];
    

    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 72;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
      
}
-(void)tapKeyBoardTypeDissmiss{
    
    [self.view endEditing:YES];

}

- (UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44) style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight           = 50;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
        }
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapKeyBoardTypeDissmiss)];
        [self.tableView addGestureRecognizer:tap];
        _tableView.showsVerticalScrollIndicator = NO;
     

    }
    return _tableView;
}

@end
